const app = getApp();

class AsynGetOpenid {
  static get_openidinfor(codecallback) {
    wx.checkSession({
      success: function (res) {

      },
      fail: function (res) {
        wx.login({
          success: res => {
            if (res.code) {
              var data = {
                'js_code': res.code,
              }
              app.post(app.globalData.serverurl+'/api/user/openid', data).then((res) => {
                console.log(res);//正确返回结果
                wx.setStorageSync('openId', res.open_id)
                var data = {
                  'open_id': res.open_id
                }
                app.post(app.globalData.serverurl+'/api/user/userinfo', data).then((res) => {
                  console.log(res);//正确返回结果
                  if (res != 'unbind') {
                    wx.setStorageSync('userId', res.userid)
                    wx.setStorageSync('cop_name', res.cop_name)
                    wx.setStorageSync('name', res.name)
                    wx.setStorageSync('cop_id', res.cop_id)
                  } else {
                    console.log(3)
                    console.log(res)
                    this.unbind() //提示未绑定

                  }
                  var data = {
                    'open_id': wx.getStorageSync('openId'),
                    'cop_id': wx.getStorageSync('cop_id')

                  }
                  app.post(app.globalData.serverurl +'/api/code/getcode', data).then((res) => {
                    console.log(res)
                    if (res == 'unbind') {
                      this.unbind() //提示未绑定

                    } else {
                      codecallback(res)
                    }
                  }).catch((errMsg) => {
                    console.log(errMsg);//错误提示信息
                  })
                }).catch((errMsg) => {
                  console.log(errMsg);//错误提示信息
                });
              }).catch((errMsg) => {
                console.log(errMsg);//错误提示信息
              });
            }
          }
        })
      }
    })
  }

  static unbind() {
    wx.showModal({
      title: '提示',
      content: '您还没有绑定',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.navigateTo(
            {
              url: '../../pages/selectpage/selectpage'
            }
          )
        }
      }
    })

  }

  static getcode(openid, cop_id, codecallback) {
    var data = {
      'open_id': openid,
      'cop_id': cop_id

    }
    app.post(app.globalData.serverurl +'/api/code/getcode', data).then((res) => {
      console.log(res)
      if (res == 'unbind') {
        this.unbind() //提示未绑定

      } else {
        codecallback(res)
      }
    }).catch((errMsg) => {
      wx.showModal({
        title: '警告通知',
        content: '网络或服务器故障'
      })
    })
  }

  static get_userinfor() {
    var data = {
      'open_id': wx.getStorageSync('openId')
    }
    app.post(app.globalData.serverurl +'/api/user/userinfo', data).then((res) => {
      console.log(res);//正确返回结果
      if (res != 'unbind') {
        wx.setStorageSync('userId', res.userid)
        wx.setStorageSync('cop_name', res.cop_name)
        wx.setStorageSync('name', res.name)
        wx.setStorageSync('cop_id', res.cop_id)
      } else {
        console.log(3)
        console.log(res)
        unbind()
      }
    }).catch((errMsg) => {
      console.log(errMsg);//错误提示信息
    })
  }
}

module.exports = AsynGetOpenid